package org.yray.tts.dic;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RegexUtils {

    private RegexUtils(){}

    public static final  String REGEX_EXT="sip:[0-9]{5}";
    public static final  String REGEX_IP_PORT="((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}:[0-9]{2,5}";
    public static final  String REGEX_IP_AT="@((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
    public static final  String REGEX_IP="((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
    public static final  Pattern RE_SKIP = Pattern.compile("[(（]\\d+\\.\\d+|[a-zA-Z0-9]+[)）]");
    private static final char[] connectors = new char[] { '+', '#', '&', '.', '_', '-' };
    public static final  String SPILT_AND="&";
    public static final  String SPILT_PARENTHESE_OR="[)）][\\|，,。./]{0,1}[(（]";
    public static final  String SPILT_PARENTHESE_AND="[)）]&[(（]";
    public static final  String SPILT_OR="[\\|，,。./]";
    public static final  String SPILT_ALL="[&\\|，,。./]";
    public static final  String SPILT_ALL_2="[&|，,。./()（）]";
    public static String findIpPort(String line) {
        if(line==null) {
            return null;
        }
        String pattern = REGEX_IP_PORT;
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);
        if(m.find())
        {
            return m.group();
        }
        return null;
    }



    public static String findOne(String line,String reg) {
        if(line==null) {
            return null;
        }
        Pattern r = Pattern.compile(reg);
        Matcher m = r.matcher(line);
        if(m.find())
        {
            return m.group();
        }
        return null;
    }


    public static String findExt(String line) {
        if(line==null) {
            return null;
        }
        String pattern = REGEX_EXT;
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);
        if(m.find())
        {
            return m.group().split(":")[1];
        }
        return null;
    }


    public static String findFirst(String source,String pattern ) {
        if(source==null) {
            return null;
        }
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(source);
        if(m.find())
        {
            return m.group();
        }
        return null;
    }

    public static boolean isChineseLetter(char ch) {
        return ch >= 0x4E00 && ch <= 0x9FA5;
    }


    public static boolean isEnglishLetter(char ch) {
        return (ch >= 0x0041 && ch <= 0x005A) || (ch >= 0x0061 && ch <= 0x007A);
    }


    public static boolean isDigit(char ch) {
        return ch >= 0x0030 && ch <= 0x0039;
    }

    /**
     * 全角 to 半角,大写 to 小写
     *
     * @param input
     *            输入字符
     * @return 转换后的字符
     */
    public static char regularize(char input) {
        if (input == 12288) {
            return 32;
        }
        else if (input > 65280 && input < 65375) {
            return (char) (input - 65248);
        }
        else if (input >= 'A' && input <= 'Z') {
            return (char) (input + 32);
        }
        return input;
    }


    public static boolean ccFind(char ch) {
        return isChineseLetter(ch)||isEnglishLetter(ch)||isDigit(ch)||isConnector(ch);
    }

    public static boolean isConnector(char ch) {
        for (char connector : connectors) {
            if (ch == connector) {
                return true;
            }
        }
        return false;
    }

    public static String[] spilt(String source)
    {
        if(StringUtils.isEmpty(source))
        {
            return new String[]{};
        }

        List<String> wordList= Arrays.stream(source.split(SPILT_ALL_2))
                .filter(wordMap->!wordMap.equals(""))
                .map(word->word.trim()).collect(Collectors.toList());
        String[] result=new String[wordList.size()];
        return wordList.toArray(result);
    }
}
